package com.pureut.storage.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pureut.storage.dto.PickingOutsourceDto;
import com.pureut.storage.dto.PickingRestDbDto;
import com.pureut.storage.dto.PickingRestHandoverDto;
import com.pureut.storage.dto.SchemeDto;
import com.pureut.storage.export.PickingOutsourceExport;
import com.pureut.storage.service.PickingOutsourceService;
import com.pureut.storage.util.ExcelUtil;
import com.pureut.storage.vo.OrderReviewVo;
import com.pureut.storage.vo.PickingOutsourceVo;
import com.pureut.storage.vo.PickingRestDbVo;
import com.pureut.storage.vo.UpdateDocVo;
import com.sale.common.core.domain.R;
import com.sale.common.core.web.controller.BaseController;
import com.sale.common.log.annotation.Log;
import com.sale.common.log.enums.BusinessType;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 委外发货控制层
 *
 * @author: CM
 * @date: 2022/12/28  10:58
 */
@RestController
@RequestMapping("/pickingOutsource")
public class PickingOutsourceController extends BaseController {

    @Resource
    PickingOutsourceService pickingOutsourceService;

    /**
     * 新增委外发货单
     */
    @SaCheckPermission("warehouse:outbound:subcontract:add")
    @Log(title = "新增委外发货", businessType = BusinessType.INSERT)
    @PostMapping("/addOutsource")
    public R<Void> addOutsource(@RequestBody PickingOutsourceVo pickingOutsourceVo) throws Exception {
        return toAjax(pickingOutsourceService.addOutsource(pickingOutsourceVo));
    }

    /**
     * 修改委外发货单
     */
    @SaCheckPermission("warehouse:outbound:subcontract:edit")
    @Log(title = "修改委外发货", businessType = BusinessType.UPDATE)
    @PostMapping("/updateOutsource")
    public R<Void> updateOutsource(@RequestBody PickingOutsourceVo pickingOutsourceVo) throws Exception {
        return toAjax(pickingOutsourceService.updateOutsource(pickingOutsourceVo));
    }

    /**
     * 关结委外发货单
     */
    @SaCheckPermission("warehouse:outbound:subcontract:close")
    @Log(title = "关结委外发货", businessType = BusinessType.UPDATE)
    @PostMapping("/closeOutsource")
    public R<Void> closeOutsource(@RequestBody PickingOutsourceVo pickingOutsourceVo) throws Exception {
        return toAjax(pickingOutsourceService.closeOutsource(pickingOutsourceVo));
    }

    /**
     * 删除单据
     */
    @SaCheckPermission("warehouse:outbound:subcontract:remove")
    @Log(title = "删除单据", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteOutsource/{ids}")
    public R<Void> deleteOutsource(@PathVariable("ids") String ids) throws Exception {
        return toAjax(pickingOutsourceService.deleteOutsource(ids));
    }

    /**
     * 提交审核
     */
    @SaCheckPermission("warehouse:outbound:subcontract:submit")
    @Log(title = "提交审核", businessType = BusinessType.UPDATE)
    @GetMapping("/submitOutsource/{ids}")
    public R<Void> submitOutsource(@PathVariable("ids") String ids) throws Exception {
        return toAjax(pickingOutsourceService.submitOutsource(ids));
    }


    /**
     * 查看单据
     */
    @SaCheckPermission("warehouse:outbound:subcontract:query")
    @GetMapping("/view/{id}")
    public R<PickingOutsourceDto> view(@PathVariable("id") Long id) throws Exception {
        return R.ok(pickingOutsourceService.view(id));
    }

    /**
     * 审核单据
     */
    @SaCheckPermission("warehouse:outbound:subcontract:examine")
    @Log(title = "审核单据", businessType = BusinessType.UPDATE)
    @PutMapping("/purchaseOrderReview")
    public R<Void> purchaseOrderReview(@Validated @RequestBody OrderReviewVo orderReviewVo) throws Exception {
        return toAjax(pickingOutsourceService.pickOutSourceReview(orderReviewVo));
    }

    /**
     * 查询委外出库
     */
    @GetMapping("/list")
    public TableDataInfo<PickingOutsourceDto> getList(PickingOutsourceVo pickingOutsourceVo, PageQuery pageQuery) throws Exception {
        //查询销售发货列表
        return pickingOutsourceService.getList(pickingOutsourceVo, pageQuery);
    }

    /**
     * 导出
     */
    @SaCheckPermission("warehouse:outbound:subcontract:export")
    @PostMapping("/export")
    public R<Void> inspectionMessageExport(HttpServletResponse response,PickingOutsourceVo pickingOutsourceVo) throws IOException {
        List<PickingOutsourceExport> infoExport = pickingOutsourceService.getInfoExport(pickingOutsourceVo);
        ExcelUtil<PickingOutsourceExport> excelUtil = new ExcelUtil<PickingOutsourceExport>(PickingOutsourceExport.class);
        excelUtil.excelWriter(response, infoExport, "委外出库信息.xlsx");
        return R.ok();
    }

    /**
     * 委外发货出库
     */
    @SaCheckPermission("warehouse:outbound:subcontract:outbound")
    @Log(title = "委外发货出库", businessType = BusinessType.UPDATE)
    @PostMapping("/out")
    public R<PickingRestDbDto> out(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return R.ok(pickingOutsourceService.out(pickingRestDbVo));
    }

    /**
     * 拣货完成
     */
    @GetMapping("/finishProduction/{id}")
    @Log(title = "拣货完成", businessType = BusinessType.UPDATE)
    public R<Void> finishProduction(@PathVariable("id") Long id) throws Exception {
        return toAjax(pickingOutsourceService.finishProduction(id));
    }

    /**
     * 拣货撤销
     */
    @GetMapping("/repealProduction/{id}")
    @Log(title = "拣货撤销", businessType = BusinessType.UPDATE)
    public R<Void> repealProduction(@PathVariable("id") Long id) throws Exception {
        return toAjax(pickingOutsourceService.repealProduction(id));
    }

    /**
     * 出库交接
     */
    @SaCheckPermission("warehouse:outbound:subcontract:handover")
    @Log(title = "出库交接", businessType = BusinessType.UPDATE)
    @PostMapping("/overProduction")
    public R<PickingRestHandoverDto> overProduction(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return R.ok(pickingOutsourceService.overProduction(pickingRestDbVo));
    }

    /**
     * 修改检验单状态Feign
     */
    @Log(title = "修改单据", businessType = BusinessType.UPDATE)
    @PostMapping("/outSourceUpdateStatus")
    public R<Void> outSourceUpdateStatus(@RequestBody UpdateDocVo updateDocVo) throws Exception {
        return toAjax(pickingOutsourceService.outSourceUpdateStatus(updateDocVo));
    }

    /**
     * 拣货提示
     */
    @GetMapping("/point/{id}")
    public R<SchemeDto> point(@PathVariable("id") Long id) throws Exception {
        return R.ok(pickingOutsourceService.point(id));
    }

//    /**
//     * 新增（委外订单fegin调用）
//     */
//    @PostMapping("/addPickingOutsource")
//    public AjaxResult addPickingOutsource(@RequestBody SubcontractReceiptOtherVo subcontractReceiptOtherVo) {
//        return AjaxResult.success(pickingOutsourceService.addPickingOutsource(subcontractReceiptOtherVo));
//    }
//
//    /**
//     * 关结委外发货单(fegin调用)
//     */
//    @PostMapping("/closeOutsourceFeign")
//    public AjaxResult closeOutsourceFeign(@RequestBody PickingOutsourceFeignVo pickingOutsourceFeignVo) throws Exception {
//        return toAjax(pickingOutsourceService.closeOutsourceFeign(pickingOutsourceFeignVo));
//    }

}
